<?php

class Log extends Public_Controller {

    public function __construct() {
        parent::__construct();
        $this->_load();
    }

    public function index() {
        $this->login();
        $this->_replacePhrase();
    }

    private function _load() {
        $this->load->helper(array('url', 'form'));
        //$this->load->model('user');
    }

    public function login() {
        if (!$this->input->is_ajax_request()) {
            redirect('product/list_product/index');
            return false;
        }

        if ((bool) $userLog = $this->input->post('user')) {
            $userLog['member_type'] = MEMBER;

            if ((bool) $this->user->login($userLog)) {

                unset($userLog['password']);
                unset($userLog['member_type']);

                return $this->return_success_json($userLog, '');
            }

            unset($userLog['password']);
            unset($userLog['member_type']);

            return $this->return_fail_json($userLog, $this->lang->line("phrase var='user.your_email_or_password_invalid'"));
        }

        return false;
    }

    public function logout() {
        if (!$this->user->logout()) {
            return false;
        }

        if (!$returnUrl) {
            $this->_redirect_index();
        }

        redirect($returnUrl);
    }

    private function _replacePhrase() {
        $this->lang->load('public/general', $this->_slang_name);
        $this->lang->load('public/login', $this->_slang_name);
        $this->fill_phrase('login', array('sloginFail' => $this->_serror), $this->lang->language);
    }
}

?>
